package com.zsc.wxm.ysh.anjo.model;

import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * 描述：
 *
 * @author playmaker
 * @since 2021/2/6 15:59
 */
@Data
public class SecurityUser implements UserDetails {

    /**
     * 当前登录的用户
     */
    private transient CurrentUser currentUser;

    /**
     * 当前权限
     */
    private List<String> permissionList;

    public SecurityUser() {

    }
    public SecurityUser(CurrentUser user) {
        if (user != null) {
            this.currentUser = user;
        }
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        Collection<GrantedAuthority> authorities = new ArrayList<>();
        for (String value : permissionList) {
            if (StringUtils.isEmpty(value)) {
                continue;
            }
            SimpleGrantedAuthority authority = new SimpleGrantedAuthority(value);
            authorities.add(authority);
        }
        return authorities;
    }

    @Override
    public String getPassword() {
        return currentUser.getPassword();
    }

    @Override
    public String getUsername() {
        return currentUser.getUsername();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
